<template>
  <section id="user-info-page">
    <article class="top">
     <w-row>
        <w-cell direction="column" align="center">
             <p style="text-align: center;margin-bottom: -1rem;"><img  class="userImg" :src='HeadImg'/></p>
             <p class="name">姓名：{{Name}}</p>
             <p class="level">剩余积分：{{Integral}}</p>
        </w-cell>
     </w-row>
    </article>
    <section class="info-msg">
    <article @click="getNotice()"><!-- @click="go('exchangetpl')"-->
      <w-field label-width="30" height="50" :show-icon="true" class="inp-row">
        <w-row slot="label">
            <w-cell width="24">
                <w-icon type="questionfill" fill="#478df5"></w-icon>
            </w-cell>
        </w-row>
        <div slot="content" class="sub-title">积分兑换</div>
       </w-field>
      </article>
      <article @click="getNotice()">
        <w-field label-width="30" height="50" :show-icon="true" class="inp-row">
            <w-row slot="label">
                <w-cell width="24">
                    <w-icon type="questionfill" fill="#478df5"></w-icon>
                </w-cell>
            </w-row>
            <div slot="content" class="sub-title">积分规则</div>
        </w-field>
      </article>
      <article @click="getNotice()">
        <w-field label-width="30" height="50"  :show-icon="true" class="inp-row">
            <w-row slot="label">
                <w-cell width="24">
                    <w-icon type="questionfill" fill="#478df5"></w-icon>
                </w-cell>
            </w-row>
            <div slot="content" class="sub-title">我的资料</div>
        </w-field>
      </article>
    </section>
  </section>
</template>

<script>
    import Vue from 'vue'
    import axios from 'axios'
    import { mapGetters, mapActions } from 'vuex';
    
    export default {
        name: 'info',
        data() {
            return {
                Name: '',
                HeadImg: '',
                Integral: ''
            }
        },
        props: {

        },
        mounted() {

            // if(localStorage.openID == "" || localStorage.openID == undefined || localStorage.openID == "undefined") {
            //      axios.get('/wx/getcodeurl', {　
            //         params: { 'Appid': 'wx27a54ca734af2cf8','redirect_uri': 'http://zqdn.aegis-s.com/wx/getwxusertoinfo' }
            //           }).then(function(response) {
            //             //引导用户根据地址获取code
            //             //location.href= response.data;
            //             let wxobj = this.GetRequest(response.data);
            //             let openid = wxobj["/user/info?openid"];
            //             localStorage.openID = openid;
            //             location.reload();
            //           }).catch(function(error) {　　
            //             console.log(error);　
            //     });
            // } else {

                //let openid = localStorage.openID;
                 let wxobj = this.GetRequest(location.hash);
                 let openid = wxobj["/user/info?openid"];
                 
                this.checkUserInfo(openid).then((response)=> {
                    let _res = response[0];
                    if(_res.msg == '0') { 
                        Wui.Toast.show('尚未注册，请先注册', 'middle');
                        setTimeout(function() {
                            location.href = '#/registtpl';
                        }, 1000);
                    } else if(_res.msg == '1') {
                        this.HeadImg = _res.userInfo.HeadImg;
                        this.Name = _res.userInfo.Name;
                        this.Integral = _res.userInfo.Integral;
                        //纪录用户信息
                        var _user = JSON.stringify(_res.userInfo);
                        localStorage.setItem('userMsg',_user);

                    } else {
                        Wui.Toast.show(_res.msg, 'middle');
                    }
                });
            
            //}
        },
        methods: {
            ...mapActions([
            'checkUserInfo'

            ]),
            go: function(path) {
                this.$router.push({name: path,params: {}});
            },
            GetRequest: function(url) {
                var url = decodeURI(url);//decodeURI(location.hash); //获取url中"?"符后的字串   
                var theRequest = {};
                if (url.indexOf("?") != -1) {   
                    var str = url.substr(1);   
                    let strs = str.split("&");   
                    for(var i = 0; i < strs.length; i ++) {   
                        theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);   
                    }   
                }  
                return theRequest;   
            },
            getNotice: function() {
                Wui.Toast.show("模块开发中", 'middle');
            }  
        }
    }
    
    // window.addEventListener("popstate", function(e) {  //回调函数中实现需要的功能
    //      Vue.prototype.$wx.closeWindow();
    // }, false);  
</script>

<style lang="scss" scoped>
  #user-info-page {
      font-size: 0.8rem;
      .top {
          //width: 18.75rem;
          width: 100%;
          background: url('../../assets/img/ex/banner.png') center no-repeat;
          background-size: 100% auto;
          padding: 1rem 0 1rem 0;
          img {
              width: 60%;
          }
          .name {
              background: url('../../assets/img/ex/name-bg.png') center no-repeat;
              color: #fff;
              padding: 1rem;
              background-size: 100% auto;
          }
          .level {
              color: #005ba8;
              font-weight: bold;
          }
      }
      .info-msg {
         .inp-row {
             padding-left: 20px;
         }
      }
      .userImg {
        border-radius: 50px;
      }
  }
</style>